const webpack = require('webpack') //获得webpack实例为了使用webpack提供的功能
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path');
const tool = require('./tool.js');

module.exports = (siteName) => {
        let copyList;
        if (!siteName) {
            copyList = [
                //目的是为了复制公用的代码文件，在开发模式和生产模式下都能适用
                {
                    from: './src/js', //复制本项目的js
                    to: './js',
                    ignore: ['.*']
                },
                {
                    from: './lib', //复制全局js
                    to: './lib',
                    ignore: ['.*']
                },
                {
                    from: './src/css', //复制全局css
                    to: './css',
                    ignore: ['*.less']
                },
                {
                    from: './src/images', //复制全局images
                    to: './images',
                    ignore: ['.*']
                }
            ]
        } else {
            copyList = [
                //目的是为了复制公用的代码文件，在开发模式和生产模式下都能适用
                {
                    from: `./sites/${siteName}/js`, //复制本项目的js
                    to: './js',
                    ignore: ['.*']
                },
                {
                    from: `./sites/${siteName}/css`, //复制全局css
                    to: './css',
                    ignore: ['*.less']
                },
                {
                    from: `./sites/${siteName}/images`, //复制全局images
                    to: './images',
                    ignore: ['.*']
                },
                {
                    from: './src/js', //复制本项目的js
                    to: './js',
                    ignore: ['.*']
                },
                {
                    from: './lib', //复制全局js
                    to: './lib',
                    ignore: ['.*']
                },
                {
                    from: './src/css', //复制全局css
                    to: './css',
                    ignore: ['*.less']
                },
                {
                    from: './src/images', //复制全局images
                    to: './images',
                    ignore: ['.*']
                }
            ]
        }
        let config = {
            entry: {
                main: siteName ? `./sites/${siteName}/main.js` : './src/main.js'
            },
            resolve: {
                extensions: ['.js', '.css', '.vue', '.less'], //自动解析确定的扩展,亲测有效
                alias: {
                    '@': path.resolve('src')
                },
                modules: ['node_modules'] //webpack 解析模块时应该搜索的目录
            },
            resolveLoader: {
                modules: [__dirname + '/loaders', __dirname + '/../node_modules']
            },
            devtool: false,
            module: {
                rules: [{
                        test: /\.html$/, //解决html上的src问题
                        use: [{
                            loader: 'html-loader',
                            options: {
                                attrs: ['img:src', 'img:data-src']
                                    // minimize: false,
                                    // removeComments: false,
                                    // collapseWhitespace: false
                            }
                        }]
                    },
                    {
                        // 使用 PostCSS 处理 CSS 文件
                        test: /\.css/,
                        use: [
                            'style-loader',
                            {
                                loader: 'css-loader',
                                options: {
                                    sourceMap: true,
                                    importLoaders: 1
                                        // modules: true
                                }
                            },
                            tool.postcssConfig
                        ]
                    },
                    {
                        test: /\.(png|jpg|gif|svg)$/,
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'images', //图片存放的位置
                            // publicPath: './images/' //图片引入的位置
                        }
                    },
                    {
                        test: /\.pug$/,
                        exclude: /node_modules/,
                        use: [{
                                    loader: 'html-loader',
                                    options: {
                                        // outputPath: 'html'
                                        // minimize: false,
                                        // removeComments: false,
                                        // collapseWhitespace: false
                                    }
                                },
                                'html-beauty',
                                'pug-html-loader'
                            ]
                            // loader: ['html-loader', 'pug-html-loader']
                    } //pug处理图片的src问题,html-loader可以处理html中的src问题
                ]
            },
            plugins: [
                new CopyWebpackPlugin(copyList)
            ],
            optimization: {
                splitChunks: {
                    cacheGroups: {
                        commons: {
                            chunks: 'initial',
                            minChunks: 2,
                            maxInitialRequests: 5,
                            minSize: 0
                        },
                        vendor: {
                            // 将第三方模块提取出来
                            test: /node_modules/,
                            chunks: 'initial',
                            name: 'vendor',
                            priority: 10, // 优先
                            enforce: true
                        }
                    }
                },
                minimizer: [
                    new UglifyJsPlugin({
                        uglifyOptions: {
                            ie8: true
                        }
                    })
                ]
            }
        }
        return config
    }
    //module.exports = config;
